<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //手写EventEmitter（发布订阅者--简单版）
        class EventEmitter{
            constructor(){
                this.events ={}
            }
            //实现订阅
            on(type,callback){
                if(!this.events){
                    this.events = Object.create(null)
                }

                if(!this.events[type]){
                    this.events[type] = [callback]
                }else{
                    this.events[type].push(callback)
                }
            }

            //触发事件
            emit(type,...rest){
                this.events[type] && this.events[type].forEach(fn => {
                   fn.apply(this,rest) 
                });
            }
        }

        const event = new EventEmitter();

        const handle = (...rest)=>{
            console.log(rest)
        }
        
        event.on('click',handle)
        event.emit('click',1,2,3,4)
        console.log(event)
    </script>
</body>
</html>